package org.karelun.gef.directeditor;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.tools.CellEditorLocator;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Text;
import org.karelun.gef.figure.NodeFigure;

public class NodeCellEditorLocator implements CellEditorLocator {

	private IFigure nodeFigure;

	public NodeCellEditorLocator(IFigure nodeFigure) {
		this.nodeFigure = nodeFigure;
	}

	public void relocate(CellEditor cellEditor) {
		Text text = (Text) cellEditor.getControl();
		Point pref = text.computeSize(SWT.DEFAULT, SWT.DEFAULT);
		if (nodeFigure instanceof NodeFigure) {
			Rectangle rect = ((NodeFigure) nodeFigure).getTextBounds();
			text.setBounds(rect.x, rect.y, pref.x + 1, pref.y + 1);
		} else if (nodeFigure instanceof Label) {
			Rectangle rect = ((Label) nodeFigure).getBounds();
			text.setBounds(rect.x, rect.y, pref.x + 1, pref.y + 1);
		}
	}

}